package hedspi.k55.sm.model;

import java.sql.Time;
import java.util.ArrayList;
import java.util.Locale;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import java.util.List;

public class Stock extends DBmanager {
	public static final String google_json_url = "https://www.google.com/finance/info?infotype=infoquoteall&q=";
	public static final String google_xml_url = "http://www.google.com/ig/api?stock=";
	public static final String google_search_url = "http://www.google.com/finance/match?matchtype=matchall&q=";
	public static final String google_new_url = "http://www.google.com/finance/company_news?q= &output=rss";
	public static final String google_chart_url = "https://www.google.com/finance/getchart?q=";
	public static final String ONE_DAY = "&p=1d&i=360";
	public static final String ONE_WEEK = "&p=7d&i=2016";
	public static final String ONE_MONTH = "&p=1M&i=86400";
	public static final String SIX_MONTH = "&p=6M&i=345600";
	public static final String ONE_YEAR = "&p=1Y&i=518400";
	public static final String FIVE_YEAR = "&p=5Y&i=1209600";
	// https://www.google.com/finance/getchart?q=XING&p=5d&i=400
	// https://www.google.com/finance/getchart?q=AAPL&p=6M&i=86400
	// 1D: i=360
	//
	/* Goole finance key */
	/* avvo : Average volume (float with multiplier, like '3.54M') */
	/* beta : Beta (float) */
	/* c : Amount of change while open (float) */
	/* ccol : (unknown) (chars) */
	/* cl : Last perc. change */
	/* cp : Change perc. while open (float) */
	/* e : Exchange (text, like 'NASDAQ') */
	/* ec : After hours last change from close (float) */
	/* eccol : (unknown) (chars) */
	/* ecp : After hours last chage perc. from close (float) */
	/* el : After. hours last quote (float) */
	/* el_cur : (unknown) (float) */
	/* elt : After hours last quote time (unknown) */
	/* eo : Exchange Open (0 or 1) */
	/* eps : Earnings per share (float) */
	/* fwpe : Forward PE ratio (float) */
	/* hi : Price high (float) */
	/* hi52 : 52 weeks high (float) */
	/* id : Company id (identifying number) */
	/* l : Last value while open (float) */
	/* l_cur : Last value at close (like 'l') */
	/* lo : Price low (float) */
	/* lo52 : 52 weeks low (float) */
	/* lt : Last value date/time */
	/* ltt : Last trade time (Same as "lt" without the data) */
	/* mc : Market cap. (float with multiplier, like '123.45B') */
	/* name : Company name (text) */
	/* op : Open price (float) */
	/* pe : PE ratio (float) */
	/* t : Ticker (text) */
	/* type : Type (i.e. 'Company') */
	/* vo : Volume (float with multiplier, like '3.54M') */
	private String symbol = "0";
	private String companyName = "0";
	private String exchange = "0";
	private String lastTradeTime = "0"; // ex :Apr 12, 4:00PM EDT
	private String change = "0"; // Amount of change while open
	private String perc_change = "0";// Change perc. while open
	private String openPrice = "0";
	private String lastPrice = "0";
	private String highPrice = "0";
	private String lowPrice = "0";
	private String volume = "0";// ex 1,64M
	private String avg_volume = "0";// Average volume (ex 2.13M)
	private String high52week = "0";// 52 weeks high
	private String low52week = "0";// 52 weeks low
	private String market_cap = "0"; // Market capitalization
	private String pe = ""; // P/E ratio - price to earning ratio
	private String fwpe = ""; // Forward PE ratio
	private String beta = "";
	private String eps = "";
	private String shares = ""; // tong so co phieu phat hanh
	private String ins_own = "";// institutions own
	private String type = ""; // company, etc...
	private String dividend = "";
	private String dividend_yield = "";
	// /////////////////////////

	// zend update: share - so co phieu, price_in - gia trung binh,portfolio_id
	// - truong muc
	private String avgprice = "0";
	private String my_shares = "0";
	private String portfolio_id = "0";

	// zend update: constructor
	/*
	 * public Stock(String name, String symbol, Double price_in, Double
	 * last_price, int shares, Double open, Double high, Double low, Double
	 * mkt_cap, Double volume) { this.companyName = new String(name);
	 * this.symbol = new String(symbol); this.avgprice = price_in.doubleValue();
	 * this.lastPrice = new String(Double.toString(last_price)); this.shares =
	 * "" + shares; this.openPrice = new String(Double.toString(open));
	 * this.highPrice = new String(Double.toString(high)); this.lowPrice = new
	 * String(Double.toString(low)); this.market_cap = new
	 * String(Double.toString(mkt_cap)); this.volume = new
	 * String(Double.toString(volume)); }
	 */

	/**
	 * Refresh information of stock, get from internet
	 */
	public boolean refresh() {
		if (symbol != null) {
			String url = makeURL(symbol);
			JsonFetch jf = new JsonFetch(url);

			getJsonContent(jf.getObject());
			return true;
		} else
			return false;
	}

	// database

	/**
	 * load from database
	 * 
	 * @return
	 */
	public boolean init() {
		if (isExist("stock", "symbol", this.symbol)) {
			Cursor cursor = doQuery("SELECT * FROM stock WHERE symbol like "
					+ "\"" + this.symbol + "\"");
			cursor.moveToFirst();
			String[] test;
			this.companyName = cursor.getString(2);
			this.exchange = cursor.getString(3);
			this.lastTradeTime = cursor.getString(4);
			this.change = cursor.getString(5);
			this.perc_change = cursor.getString(6);
			test = cursor.getString(7).split("/");
			this.openPrice = test[0];
			this.lastPrice = test[1];
			test = cursor.getString(8).split("/");
			this.highPrice = test[0];
			this.lowPrice = test[1];
			this.volume = cursor.getString(9);
			this.avg_volume = cursor.getString(10);
			test = cursor.getString(11).split("/");
			this.high52week = test[0];
			this.low52week = test[1];
			this.market_cap = cursor.getString(12);
			this.pe = cursor.getString(13);
			this.beta = cursor.getString(14);
			this.eps = cursor.getString(15);
			this.shares = cursor.getString(16);
			if (cursor.getString(17).length() == 1) {
				this.dividend = "0";
				this.dividend_yield = "0";
			} else {
				test = cursor.getString(17).split("/");
				this.dividend = test[0];
				this.dividend_yield = test[1];
			}
			this.avgprice = cursor.getString(18);
			this.my_shares = cursor.getString(19);

			return true;
		}
		return false;
	}

	public Stock(Context context, String symbol) {
		super(context);
		this.symbol = symbol;
	}

	public boolean update() {
		if (isExist("stock", "symbol", this.symbol)) {
			// update
			SQLiteDatabase db = this.getWritableDatabase();
			ContentValues value = new ContentValues();

			value.put("symbol", this.symbol);
			value.put("companyname", this.companyName);
			value.put("exchange", this.exchange);
			value.put("lasttradetime", this.lastTradeTime);
			value.put("change", this.change);
			value.put("perc_change", this.perc_change);
			value.put("openlastprice", this.openPrice + "/" + this.lastPrice);
			value.put("highlowprice", this.highPrice + "/" + this.lowPrice);
			value.put("volume", this.volume);
			value.put("avgvolume", this.avg_volume);
			value.put("highlow52week", this.high52week + "/" + this.low52week);
			value.put("market_cap", this.market_cap);
			value.put("pe", this.pe);
			value.put("beta", this.beta);
			value.put("eps", this.eps);
			value.put("shares", this.shares);
			value.put("dividendyield", this.dividend + "/"
					+ this.dividend_yield);
			value.put("avgprice", this.avgprice);
			value.put("myshare", this.my_shares);

			int affect = (int) db.update("stock", value, "symbol = ?",
					new String[] { "\"" + this.symbol + "\"" });
			if (affect != 1) {
				return false;
			}
			return true;
		} else {
			// add
			SQLiteDatabase db = this.getWritableDatabase();
			ContentValues value = new ContentValues();

			value.put("symbol", this.symbol);
			value.put("companyname", this.companyName);
			value.put("exchange", this.exchange);
			value.put("lasttradetime", this.lastTradeTime);
			value.put("change", this.change);
			value.put("perc_change", this.perc_change);
			value.put("openlastprice", this.openPrice + "/" + this.lastPrice);
			value.put("highlowprice", this.highPrice + "/" + this.lowPrice);
			value.put("volume", this.volume);
			value.put("avgvolume", this.avg_volume);
			value.put("highlow52week", this.high52week + "/" + this.low52week);
			value.put("market_cap", this.market_cap);
			value.put("pe", this.pe);
			value.put("beta", this.beta);
			value.put("eps", this.eps);
			value.put("shares", this.shares);
			value.put("dividendyield", this.dividend + "/"
					+ this.dividend_yield);
			value.put("avgprice", this.avgprice);
			value.put("myshare", this.my_shares);

			int affect = (int) db.insert("stock", null, value);
			if (affect == -1) {
				return false;
			}
			db.close();
			return true;
		}
	}

	public List<Transaction> getAllTransaction() {
		SQLiteDatabase db = this.getReadableDatabase();
		List<Transaction> list = new ArrayList<Transaction>();
		String query = "SELECT * FROM _transaction WHERE symbol like " + "\""
				+ this.symbol + "\"";
		Cursor cursor = db.rawQuery(query, null);
		if (cursor != null) {

			if (cursor.moveToFirst()) {

				do {
					Transaction trans = new Transaction(context,
							Integer.parseInt(cursor.getString(1)),
							cursor.getString(2), cursor.getString(3),
							Integer.parseInt(cursor.getString(4)),
							Double.parseDouble(cursor.getString(5)),
							Double.parseDouble(cursor.getString(6)),
							cursor.getString(7));
					list.add(trans);
				} while (cursor.moveToNext());
				db.close();
				return list;
			}
		}

		db.close();
		return null;
	}

	/*
	 * // add to Database public boolean create() { DBmanager myDB = new
	 * DBmanager(null); SQLiteDatabase db = myDB.getWritableDatabase();
	 * ContentValues value = new ContentValues(); // viet lai ham ktra
	 * value.put(DBmanager._NAME, this.companyName);
	 * value.put(DBmanager._SYMBOL, this.symbol);
	 * value.put(DBmanager._PORTFOLIO_ID, this.portfolio_id);
	 * value.put(DBmanager._EXCHANGE_ID, 1); // viet lai ham getExchangeID
	 * value.put(DBmanager._PRICE_IN, this.price_in); // check lai
	 * value.put(DBmanager._LAST_PRICE, this.lastPrice);
	 * value.put(DBmanager._SHARES, this.my_shares); value.put(DBmanager._OPEN,
	 * this.openPrice); value.put(DBmanager._HIGH, this.highPrice);
	 * value.put(DBmanager._LOW, this.lowPrice); value.put(DBmanager._MKT_CAP,
	 * this.market_cap); value.put(DBmanager._VOLUME, this.volume);
	 * 
	 * db.insert(DBmanager.TABLE_STOCK, null, value); db.close(); return true; }
	 * 
	 * // read from Database public boolean read() { if (this.symbol == null) {
	 * return false; } else { DBmanager myDB = new DBmanager(null);
	 * SQLiteDatabase db = myDB.getWritableDatabase(); Cursor cursor =
	 * db.query(DBmanager.TABLE_STOCK, new String[] { DBmanager._ID,
	 * DBmanager._NAME, DBmanager._SYMBOL, DBmanager._PORTFOLIO_ID,
	 * DBmanager._EXCHANGE_ID, DBmanager._PRICE_IN, DBmanager._LAST_PRICE,
	 * DBmanager._OPEN, DBmanager._HIGH, DBmanager._LOW, DBmanager._MKT_CAP,
	 * DBmanager._VOLUME }, DBmanager._SYMBOL + "=? and " +
	 * DBmanager._PORTFOLIO_ID + "=?", new String[] { this.symbol,
	 * Integer.valueOf(portfolio_id).toString() }, null, null, null); if (cursor
	 * != null) { cursor.moveToFirst(); this.companyName = "" +
	 * cursor.getString(1); this.symbol = "" + cursor.getString(2);
	 * this.price_in = Double.parseDouble(cursor.getString(5)); this.lastPrice =
	 * "" + cursor.getString(6); this.my_shares =
	 * Integer.parseInt(cursor.getString(7)); this.openPrice = "" +
	 * cursor.getString(8); this.highPrice = "" + cursor.getString(9);
	 * this.lowPrice = "" + cursor.getString(10); this.market_cap = "" +
	 * cursor.getString(11); this.volume = "" + cursor.getString(12); return
	 * true; } return false; }
	 * 
	 * }
	 * 
	 * // remove from Database public boolean delete() { if (this.symbol ==
	 * null) { return false; } else { DBmanager myDB = new DBmanager(null);
	 * SQLiteDatabase db = myDB.getWritableDatabase();
	 * db.delete(DBmanager.TABLE_STOCK, DBmanager._SYMBOL + "=?", new String[] {
	 * this.symbol }); db.close(); return true; } }
	 * 
	 * // update to Database public boolean update() { if (this.symbol == null)
	 * { return false; } else { DBmanager myDB = new DBmanager(null);
	 * SQLiteDatabase db = myDB.getWritableDatabase(); ContentValues value = new
	 * ContentValues();
	 * 
	 * value.put(DBmanager._NAME, this.companyName);
	 * value.put(DBmanager._SYMBOL, this.symbol);
	 * value.put(DBmanager._PORTFOLIO_ID, this.portfolio_id);
	 * value.put(DBmanager._EXCHANGE_ID, 1); // viet lai ham getExchangeID
	 * value.put(DBmanager._PRICE_IN, this.price_in); // check lai
	 * value.put(DBmanager._LAST_PRICE, this.lastPrice);
	 * value.put(DBmanager._SHARES, this.shares); // check again
	 * value.put(DBmanager._OPEN, this.openPrice); value.put(DBmanager._HIGH,
	 * this.highPrice); value.put(DBmanager._LOW, this.lowPrice);
	 * value.put(DBmanager._MKT_CAP, this.market_cap);
	 * value.put(DBmanager._VOLUME, this.volume);
	 * 
	 * int affectrows; affectrows = db.update(DBmanager.TABLE_STOCK, value,
	 * DBmanager._SYMBOL + "=? and" + DBmanager._PORTFOLIO_ID + "=?", new
	 * String[] { this.symbol, Integer.valueOf(this.portfolio_id).toString() });
	 * if (affectrows == 1) { return true; } return false; } }
	 * 
	 * // public boolean move(int portfolio_id) { if (this.symbol == null) {
	 * return false; } else { if (this.setPortfolio(portfolio_id)) { if
	 * (this.update()) { return true; } } return false; } }
	 */
	public String makeURL(String symbol) {
		StringBuffer sb = new StringBuffer(google_json_url);
		sb.append(symbol);
		return sb.toString();
	}

	// ht : get content of object received.
	private void getJsonContent(JSONObject json) {
		if (json == null)
			return;
		try {
			symbol = json.getString("t");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			exchange = json.getString("e");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		try {
			lastPrice = json.getString("l");
		} catch (JSONException e) {
			e.printStackTrace();
		}
		try {
			lastTradeTime = json.getString("lt");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			change = json.getString("c");
		} catch (JSONException e) {
			e.printStackTrace();
		}
		try {
			perc_change = json.getString("cp");
		} catch (JSONException e) {
			e.printStackTrace();
		}
		try {
			openPrice = json.getString("op");
		} catch (JSONException e) {
			e.printStackTrace();
		}
		try {
			highPrice = json.getString("hi");
		} catch (JSONException e) {
			e.printStackTrace();
		}
		try {
			lowPrice = json.getString("lo");
		} catch (JSONException e) {
			e.printStackTrace();
		}
		try {
			volume = json.getString("vo");
		} catch (JSONException e) {
			e.printStackTrace();
		}
		try {
			avg_volume = json.getString("avvo");
		} catch (JSONException e) {
			e.printStackTrace();
		}
		try {
			high52week = json.getString("hi52");
		} catch (JSONException e) {
			e.printStackTrace();
		}
		try {
			low52week = json.getString("lo52");
		} catch (JSONException e) {
			e.printStackTrace();
		}
		try {
			market_cap = json.getString("mc");
		} catch (JSONException e) {
			e.printStackTrace();
		}
		try {
			pe = json.getString("pe");
		} catch (JSONException e) {
			e.printStackTrace();
		}
		try {
			fwpe = json.getString("fwpe");
		} catch (JSONException e) {
			e.printStackTrace();
		}
		try {
			beta = json.getString("beta");
		} catch (JSONException e) {
			e.printStackTrace();
		}
		try {
			eps = json.getString("eps");
		} catch (JSONException e) {
			e.printStackTrace();
		}
		try {
			shares = json.getString("shares");
		} catch (JSONException e) {
			e.printStackTrace();
		}
		try {
			ins_own = json.getString("inst_own");
		} catch (JSONException e) {
			e.printStackTrace();
		}
		try {
			companyName = json.getString("name");
		} catch (JSONException e) {
			e.printStackTrace();
		}
		try {
			type = json.getString("type");
		} catch (JSONException e) {
			e.printStackTrace();
		}
		try {
			dividend = json.getString("div");
		} catch (JSONException e) {
			e.printStackTrace();
		}
		try {
			dividend_yield = json.getString("yld");
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	// zend:

	public String getSymbol() {
		return symbol;
	}

	public boolean setSymbol() {
		return false;
	}

	// Zend update
	public String getCompanyName() {
		return this.companyName;
	}

	public String getExchange() {
		return this.exchange;
	}

	public String getLastPrice() {
		return lastPrice;
	}

	public String getOpen() {
		return openPrice;
	}

	public String getHigh() {
		return highPrice;
	}

	public String getHigh52week() {
		return high52week;
	}

	public String getLow() {
		return lowPrice;
	}

	public String getLow52week() {
		return low52week;
	}

	public String getChange() {
		return change;
	}

	public String getPercChange() {
		return perc_change;
	}

	public Double getTotal() {
		return (Double.parseDouble(this.my_shares) * Double
				.parseDouble(this.avgprice));
	}

	public String getMyShares() {
		return this.my_shares;
	}

	public String getTradeTime() {
		return this.lastTradeTime;
	}

	public String getAvgPrice() {
		return this.avgprice;
	}

	public String getAvgVolume() {
		return avg_volume;
	}

	// end update
	public String getPE() {
		return pe;
	}

	public String getfwPE() {
		return fwpe;
	}

	public String getEPS() {
		return eps;
	}

	public String getBeta() {
		return beta;
	}

	public String getRange() {
		return null;
	}

	public String getShares() {
		return shares;
	}

	public String getInstOwn() {
		return ins_own;
	}

	public String getType() {
		return type;
	}

	public String getVolume() {
		return volume;
	}

	public String getMarketCap() {
		return market_cap;
	}

	public boolean isEmpty() {
		return false;
	}

	public String getPrice() {
		return this.lastPrice;
	}

	public String getDividend() {
		return dividend;
	}

	public String getDividendYield() {
		return dividend_yield;
	}

	public void setMyShares(String myshares) {
		this.my_shares = myshares;
	}
	public void setAvgPrice(String avgprice){
		this.avgprice = avgprice;
	}
	public String getURLChart(String period) {
		StringBuffer buff = new StringBuffer(google_chart_url);
		buff.append(getSymbol().toUpperCase(Locale.ENGLISH));
		buff.append(period);
		return buff.toString();
	}
	public Double getCostBasic(){
		return Double.parseDouble(this.avgprice) * Double.parseDouble(this.my_shares);
	}
	public Double getMktValue(){
		return Double.parseDouble(this.lastPrice) * Double.parseDouble(this.my_shares);
	}
	public Double getAnnual(){
		Double annual = (Double.parseDouble(this.lastPrice) - Double.parseDouble(this.avgprice))* Double.parseDouble(this.my_shares);
		return annual;
	}
	public Double getAnnaulPerc(){
		Double annual_perc = getAnnual() / (Double.parseDouble(this.my_shares) * Double.parseDouble(this.avgprice));
		return annual_perc;
	}
}
